"""
匹兹堡睡眠质量指数（PSQI）是一种常用的睡眠评估工具，包含多个问题项，用于评估睡眠质量。
一共9题，q1、q3、q4是填空，其余都是4个选项（0分、1分、2分、3分）。其中q5又分为a-j一共10个题，每题也是4个选项。
记分方式：A~G评分也分为0分、1分、2分、3分四个选项
A、睡眠质量：q6计分（A评分与q6一致）
B、入睡时间：q2+q5a=0分：0分、1~2分：1分、3-4分：2分、5-6分：3分
C、睡眠时间：q4计分（q4>7：0分、6<q4<=7：1分、5<q4<=6：2分、q4<5：3分
D、睡眠效率：q4/(24-q1+q3)*100%>85%：0分、75%~85%（不含75%）：1分、65%~75%（含75%）：2分、<65%：3分
E、睡眠障碍：q5b~q5j计分累计0分：0分、1-9分：1分、10-18分：2分、19-27分：3分
F、催眠药物：q7计分（F评分与q7一致）
G、日渐功能障碍：q8+q9=0分：0分、1-2分：1分、3-4分：2分、5-6分：3分
每题评分范围为0~3分，总分0~21分。得分越高，说明睡眠质量越差。总分≤5分，代表睡眠质量好，总分＞5分，代表睡眠质量差。
"""
def calculate_psqi(answers):
    # 初始化各个部分的分数
    component_scores = {
        'A': 0,  # 睡眠质量
        'B': 0,  # 入睡时间
        'C': 0,  # 睡眠时间
        'D': 0,  # 睡眠效率
        'E': 0,  # 睡眠障碍
        'F': 0,  # 催眠药物
        'G': 0   # 日渐功能障碍
    }

    # 计算各个部分的分数
    # A、睡眠质量
    component_scores['A'] = answers['q6']

    # B、入睡时间
    sum_b = answers['q2'] + answers['q5a']
    if sum_b == 0:
        component_scores['B'] = 0
    elif 1 <= sum_b <= 2:
        component_scores['B'] = 1
    elif 3 <= sum_b <= 4:
        component_scores['B'] = 2
    elif 5 <= sum_b <= 6:
        component_scores['B'] = 3

    # C、睡眠时间
    if answers['q4'] > 7:
        component_scores['C'] = 0
    elif 6 < answers['q4'] <= 7:
        component_scores['C'] = 1
    elif 5 < answers['q4'] <= 6:
        component_scores['C'] = 2
    elif answers['q4'] < 5:
        component_scores['C'] = 3

    # D、睡眠效率
    sleep_efficiency = (answers['q4'] / (24 + answers['q3'] - answers['q1'])) * 100 if (answers['q3'] - answers['q1']) != 0 else 0
    print(sleep_efficiency)
    if sleep_efficiency > 85:
        component_scores['D'] = 0
    elif 75 < sleep_efficiency <= 85:
        component_scores['D'] = 1
    elif 65 <= sleep_efficiency <= 75:
        component_scores['D'] = 2
    elif sleep_efficiency < 65:
        component_scores['D'] = 3

    # E、睡眠障碍
    sum_e = sum(answers[f'q5{chr(98 + i)}'] for i in range(9))
    if sum_e == 0:
        component_scores['E'] = 0
    elif 1 <= sum_e <= 9:
        component_scores['E'] = 1
    elif 10 <= sum_e <= 18:
        component_scores['E'] = 2
    elif 19 <= sum_e <= 27:
        component_scores['E'] = 3

    # 催眠药物
    component_scores['F'] = answers['q7']

    # G、日渐功能障碍
    sum_g = answers['q8'] + answers['q9']
    if sum_g == 0:
        component_scores['G'] = 0
    elif 1 <= sum_g <= 2:
        component_scores['G'] = 1
    elif 3 <= sum_g <= 4:
        component_scores['G'] = 2
    elif 5 <= sum_g <= 6:
        component_scores['G'] = 3

    # 计算总分
    total_score = sum(component_scores.values())

    # 判断睡眠质量
    if total_score <= 5:
        sleep_quality = "好"
    else:
        sleep_quality = "差"

    return {
        'total_score': total_score,
        'sleep_quality': sleep_quality,
        'component_scores': component_scores
    }

# # 示例输入
# answers = {
#     'q1': 22,
#     'q2': 1,
#     'q3': 10,
#     'q4': 8,
#     'q5a': 0,
#     'q5b': 1,
#     'q5c': 2,
#     'q5d': 3,
#     'q5e': 0,
#     'q5f': 1,
#     'q5g': 2,
#     'q5h': 3,
#     'q5i': 0,
#     'q5j': 1,
#     'q6': 2,
#     'q7': 1,
#     'q8': 2,
#     'q9': 3
# }
#
# # 计算PSQI评分
# result = calculate_psqi(answers)
# print(result)